package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjlsfssfjlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlsfssfjlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlCjlsfssfjlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlCjlsfssfjlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlCjlsfssfjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料筛分记录（水筛法）详情Service业务层处理
 *
 * @author tll
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjlsfssfjlbBaDetailServiceImpl implements IReportJlCjlsfssfjlbBaDetailService {

    private final ReportJlCjlsfssfjlbBaDetailMapper baseMapper;

    /**
     * 查询粗集料筛分记录（水筛法）详情
     */
    @Override
    public ReportJlCjlsfssfjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询粗集料筛分记录（水筛法）详情列表
     */
    @Override
    public TableDataInfo<ReportJlCjlsfssfjlbBaDetailVo> queryPageList(ReportJlCjlsfssfjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjlsfssfjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjlsfssfjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料筛分记录（水筛法）详情列表
     */
    @Override
    public List<ReportJlCjlsfssfjlbBaDetailVo> queryList(ReportJlCjlsfssfjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlCjlsfssfjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjlsfssfjlbBaDetail> buildQueryWrapper(ReportJlCjlsfssfjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjlsfssfjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlCjlsfssfjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShaifhzzlItem1() != null, ReportJlCjlsfssfjlbBaDetail::getShaifhzzlItem1, bo.getShaifhzzlItem1());
        lqw.eq(bo.getKoucshsyzzlItem1() != null, ReportJlCjlsfssfjlbBaDetail::getKoucshsyzzlItem1, bo.getKoucshsyzzlItem1());
        lqw.eq(bo.getSunhlItem1() != null, ReportJlCjlsfssfjlbBaDetail::getSunhlItem1, bo.getSunhlItem1());
        lqw.eq(bo.getShaifhzzlItem2() != null, ReportJlCjlsfssfjlbBaDetail::getShaifhzzlItem2, bo.getShaifhzzlItem2());
        lqw.eq(bo.getKoucshsyzzlItem2() != null, ReportJlCjlsfssfjlbBaDetail::getKoucshsyzzlItem2, bo.getKoucshsyzzlItem2());
        lqw.eq(bo.getSunhlItem2() != null, ReportJlCjlsfssfjlbBaDetail::getSunhlItem2, bo.getSunhlItem2());
        lqw.eq(StringUtils.isNotBlank(bo.getShaikcc()), ReportJlCjlsfssfjlbBaDetail::getShaikcc, bo.getShaikcc());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem1()), ReportJlCjlsfssfjlbBaDetail::getChenglcsItem1, bo.getChenglcsItem1());
        lqw.eq(bo.getZhil3Item1() != null, ReportJlCjlsfssfjlbBaDetail::getZhil3Item1, bo.getZhil3Item1());
        lqw.eq(bo.getZhil4Item1() != null, ReportJlCjlsfssfjlbBaDetail::getZhil4Item1, bo.getZhil4Item1());
        lqw.eq(bo.getShaiszlItem1() != null, ReportJlCjlsfssfjlbBaDetail::getShaiszlItem1, bo.getShaiszlItem1());
        lqw.eq(bo.getFenjsyItem1() != null, ReportJlCjlsfssfjlbBaDetail::getFenjsyItem1, bo.getFenjsyItem1());
        lqw.eq(bo.getLeijsyItem1() != null, ReportJlCjlsfssfjlbBaDetail::getLeijsyItem1, bo.getLeijsyItem1());
        lqw.eq(bo.getTonggbflItem1() != null, ReportJlCjlsfssfjlbBaDetail::getTonggbflItem1, bo.getTonggbflItem1());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem2()), ReportJlCjlsfssfjlbBaDetail::getChenglcsItem2, bo.getChenglcsItem2());
        lqw.eq(bo.getZhil3Item2() != null, ReportJlCjlsfssfjlbBaDetail::getZhil3Item2, bo.getZhil3Item2());
        lqw.eq(bo.getZhil4Item2() != null, ReportJlCjlsfssfjlbBaDetail::getZhil4Item2, bo.getZhil4Item2());
        lqw.eq(bo.getShaiszlItem2() != null, ReportJlCjlsfssfjlbBaDetail::getShaiszlItem2, bo.getShaiszlItem2());
        lqw.eq(bo.getFenjsyItem2() != null, ReportJlCjlsfssfjlbBaDetail::getFenjsyItem2, bo.getFenjsyItem2());
        lqw.eq(bo.getLeijsyItem2() != null, ReportJlCjlsfssfjlbBaDetail::getLeijsyItem2, bo.getLeijsyItem2());
        lqw.eq(bo.getTonggbflItem2() != null, ReportJlCjlsfssfjlbBaDetail::getTonggbflItem2, bo.getTonggbflItem2());
        lqw.eq(bo.getTonggbflPjz() != null, ReportJlCjlsfssfjlbBaDetail::getTonggbflPjz, bo.getTonggbflPjz());
        return lqw;
    }

    /**
     * 新增粗集料筛分记录（水筛法）详情
     */
    @Override
    public Boolean insertByBo(ReportJlCjlsfssfjlbBaDetailBo bo) {
        ReportJlCjlsfssfjlbBaDetail add = BeanUtil.toBean(bo, ReportJlCjlsfssfjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改粗集料筛分记录（水筛法）详情
     */
    @Override
    public Boolean updateByBo(ReportJlCjlsfssfjlbBaDetailBo bo) {
        ReportJlCjlsfssfjlbBaDetail update = BeanUtil.toBean(bo, ReportJlCjlsfssfjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjlsfssfjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料筛分记录（水筛法）详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
